<?php

class AdminControl extends Control {

	public function index()
	{
		self::userlist();
	}

	public function userlist()
	{
		$page = $_REQUEST["page"];
		$pagesize = $_REQUEST["pagesize"];
		$page = intval($page);
		$page = $page ? $page : 1;
		$pagesize = intval($pagesize);
		$pagesize = $pagesize ? $pagesize : 20;

        $users = modcall("admin", "ulist", array($page, $pagesize));
        $content = load_view("admin-userlist", array("res" => $users), false);
        load_view("admin-tpl", array("content" => $content));
    }

	public function useredit()
	{
		$uid = intval($_REQUEST["uid"]);
		$uinfo = daocall("user", "getuserext", array($uid));
		$content = load_view("admin-useredit", array("row" => $uinfo, "uid" => $uid), false);
        load_view("admin-tpl", array("content" => $content));
	}

	public function userupdate()
	{
		$uid = intval($_REQUEST["uid"]);
		$xp = intval($_REQUEST["xp"]);
		$gb = intval($_REQUEST["gb"]);
		$mb = intval($_REQUEST["mb"]);
		$price = intval($_REQUEST["price"]);
		$side = trim($_REQUEST["side"]);

		$err = 1;
		if(!daocall("user", "setuserext", array($uid, 
				array(
					"xp" => $xp,
					"gb" => $gb,
					"mb" => $mb,
					"price" => $price,
					"side" => $side,
				)
			))) $err = 2;

		self::userlist();
	}

	public function herolist()
	{
		$uid = intval($_REQUEST["uid"]);

        $heros = daocall("hero", "getheros", array($uid));

        $content = load_view("admin-herolist", array("res" => $heros, "uid" => $uid), false);
        load_view("admin-tpl", array("content" => $content));
	}

	public function heroedit()
	{
		$uid = intval($_REQUEST["uid"]);
		$hid = intval($_REQUEST["hid"]);
		$heros = daocall("hero", "getheros", array($uid));
		if($heros && $heros[$hid]) $hero = $heros[$hid];
		$content = load_view("admin-heroedit", array("row" => $hero, "uid" => $uid), false);
        load_view("admin-tpl", array("content" => $content));
	}

	public function heroupdate()
	{
		$uid = intval($_REQUEST["uid"]);
		$hid = intval($_REQUEST["hid"]);
		$xp = intval($_REQUEST["xp"]);
		$hsn = trim($_REQUEST["hsn"]);

		$err = 1;
		if(!daocall("hero", "sethero", array($uid, $hid,
				array(
					"xp" => $xp,
					"hsn" => $hsn,
				)
			))) $err = 2;

		self::herolist();
	}

	public function heronew()
	{
		$uid = intval($_REQUEST["uid"]);
		$content = load_view("admin-heronew", array("uid" => $uid), false);
        load_view("admin-tpl", array("content" => $content));
	}

	public function herodonew()
	{
		$uid = intval($_REQUEST["uid"]);
		$xp = intval($_REQUEST["xp"]);
		$hsn = trim($_REQUEST["hsn"]);

		$err = 1;
		if(!daocall("hero", "newhero", array($uid,
				array(
					"xp" => $xp,
					"hsn" => $hsn,
				)
			))) $err = 2;

		self::herolist();
	}

	public function herododel()
	{
		$uid = intval($_REQUEST["uid"]);
		$hid = intval($_REQUEST["hid"]);

		daocall("equip", "clearhid", array($uid));
		daocall("hero", "delhero", array($uid, $hid));

		self::herolist();
	}

	public function hgrouplist()
	{
		$uid = intval($_REQUEST["uid"]);

        $res = daocall("herogroup", "getherogroups", array($uid));

        $content = load_view("admin-hgrouplist", array("res" => $res, "uid" => $uid), false);
        load_view("admin-tpl", array("content" => $content));
	}

	public function hgroupedit()
	{
		$uid = intval($_REQUEST["uid"]);
		$gid = intval($_REQUEST["gid"]);
		$res = daocall("herogroup", "getherogroups", array($uid));
		if($res && $res[$gid]) $row = $res[$gid];
		$content = load_view("admin-hgroupedit", array("row" => $row, "uid" => $uid), false);
        load_view("admin-tpl", array("content" => $content));
	}

	public function hgroupupdate()
	{
		$uid = intval($_REQUEST["uid"]);
		$gid = intval($_REQUEST["gid"]);
		$lv = intval($_REQUEST["lv"]);
		$stat = trim($_REQUEST["stat"]);
		$grid = $_REQUEST["grid"];

		$gridres = array();
		if($grid)
		{
			foreach($grid as $gg)
			{
				if(!$gg) continue;

				$gg = explode(":", $gg);
				if(!$gg || count($gg) != 2) continue;

				$gg[0] = intval($gg[0]);
				$gg[1] = intval($gg[1]);
				if(!$gg[0]) continue;

				$gridres[$gg[0]] = $gg[1];
			}
		}

		$err = 1;
		if(!daocall("herogroup", "setherogroup", array($uid, $gid,
				array(
					"lv" => $lv,
					"stat" => $stat,
					"grid" => $gridres,
				)
			))) $err = 2;

		self::hgrouplist();
	}

	public function hgroupnew()
	{
		$uid = intval($_REQUEST["uid"]);
		$content = load_view("admin-hgroupnew", array("uid" => $uid), false);
        load_view("admin-tpl", array("content" => $content));
	}

	public function hgroupdonew()
	{
		$uid = intval($_REQUEST["uid"]);
		$gid = intval($_REQUEST["gid"]);
		$lv = intval($_REQUEST["lv"]);
		$stat = trim($_REQUEST["stat"]);
		$grid = $_REQUEST["grid"];

		$gridres = array();
		if($grid)
		{
			foreach($grid as $gg)
			{
				if(!$gg) continue;

				$gg = explode(":", $gg);
				if(!$gg || count($gg) != 2) continue;

				$gg[0] = intval($gg[0]);
				$gg[1] = intval($gg[1]);
				if(!$gg[0]) continue;

				$gridres[$gg[0]] = $gg[1];
			}
		}

		$err = 1;
		if(!daocall("herogroup", "newherogroup", array($uid, $gid,
				array(
					"lv" => $lv,
					"stat" => $stat,
					"grid" => $gridres,
				)
			))) $err = 2;

		self::hgrouplist();
	}

	public function hgroupdodel()
	{
		$uid = intval($_REQUEST["uid"]);
		$gid = intval($_REQUEST["gid"]);

		$err = 1;
		if(!daocall("herogroup", "delherogroup", array($uid, $gid))) $err = 2;

		self::hgrouplist();
	}

	public function stufflist()
	{
		$uid = intval($_REQUEST["uid"]);

        $res = &daocall("stuff", "getstuffs", array($uid));
        $content = load_view("admin-stufflist", array("res" => $res, "uid" => $uid), false);
        load_view("admin-tpl", array("content" => $content));
    }

	public function equiplist()
	{
		$uid = intval($_REQUEST["uid"]);

        $res = &daocall("equip", "getequips", array($uid));
        $content = load_view("admin-equiplist", array("res" => $res, "uid" => $uid), false);
        load_view("admin-tpl", array("content" => $content));
    }
}

?>
